<template>
  <el-dialog
    width="25%"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="商品id" prop="productId">
      <el-select v-model="dataForm.productId" placeholder="请选择" @change="getStock(dataForm.productId)">
        <el-option
          v-for="item in products"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="开始时间" prop="sessionId">
      <el-select v-model="dataForm.sessionId" placeholder="请选择">
        <el-option
          v-for="item in sessions"
            :key="item.id"
            :label="item.startTime+'--'+item.endTime"
            :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="秒杀价格" prop="seckillPrice">
      <el-input-number v-model="dataForm.seckillPrice" :precision="2" :step="0.1" placeholder="秒杀价格"></el-input-number>

    </el-form-item>
    <el-form-item label="秒杀商品数量" prop="seckillCount">
      <el-input-number v-model="dataForm.seckillCount"  :min="1" :max="this.dataForm.stock" label="秒杀商品数量"></el-input-number>
    </el-form-item>
    <el-form-item label="最大购买数量" prop="seckillLimit">
      <el-input-number v-model="dataForm.seckillLimit"  :min="1" :max="this.dataForm.seckillCount" label="最大购买数量"></el-input-number>
    </el-form-item>
    <el-form-item label="秒杀状态" prop="seckillStatus">
      <el-switch
        v-model="dataForm.seckillStatus"
        active-color="green"
        inactive-color="red"
        :active-value="1"
        :inactive-value="0"
        placeholder="秒杀状态">
      </el-switch>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import request from "@/utils/request";
  export default {
    
    data () {
      return {
        visible: false,
        sessions: [
          {
            id:0,
            startTime:'',
            endTime:''
          }
        ],
        products:[
          {
            id:0,
            name:''
          }
        ],
        dataForm: {
          id: 0,
          productId: '',
          sessionId: '', 
          stock:0,
          seckillPrice: 0,
          seckillCount: 0,
          seckillLimit: 0,
          seckillStatus: 0
        },
        dataRule: {
          productId: [
            { required: true, message: '商品不能为空', trigger: 'blur' }
          ],
          sessionId: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          seckillPrice: [
            { required: true, message: '秒杀价格不能为空', trigger: 'blur' }
          ],
          seckillCount: [
            { required: true, message: '秒杀商品数量不能为空', trigger: 'blur' }
          ],
          seckillLimit: [
            { required: true, message: '购买数量最大限制不能为空', trigger: 'blur' }
          ],
          seckillStatus: [
            { required: true, message: '秒杀状态不能为空', trigger: 'blur' }
          ]
        }
      }
      
    },
  
    components: {},
    methods: {
    
      getProduct() {
        request.get('/product/admin/products').then((response) => {
          if (response.data && response.data.code === 200) {
            this.products = response.data.products
          } else {
            this.products = []
          }
        }).catch((error) => {
          console.error(error)
        })
      },
      getSession() {
        request.get('/seckill/session/admin/getSessions').then((response) => {
          if (response.data && response.data.code === 200) {
            this.sessions = response.data.sessions
          } else {
            this.sessions = []
          }
        }).catch((error) => {
          console.error(error)
        })
      },
      getStock(productId){
        request.get(`/product/admin/getStock/${productId}`).then((response)=>{
          if (response.data && response.data.code === 200) {  
                this.dataForm.stock = response.data.stock 
                console.log("jeijf")
          }else {
            this.dataForm.stock = 0
          }
        }).catch((error) => {
          console.error(error)
        })  
      },
      init (id) {
        this.getSession()
        this.getProduct()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            request.get(`/seckill/relation/admin/info/${this.dataForm.id}`).then((response) => {  
              if (response.data && response.data.code === 200) {  
                this.dataForm.productId = response.data.relation.productId
                this.dataForm.sessionId = response.data.relation.sessionId  
                this.dataForm.seckillPrice = response.data.relation.seckillPrice  
                this.dataForm.seckillCount = response.data.relation.seckillCount  
                this.dataForm.seckillLimit = response.data.relation.seckillLimit  
                this.dataForm.seckillStatus = response.data.relation.seckillStatus  
              }  
            })
          }
        })
        this.getStock(this.dataForm.productId)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            request.post(`/seckill/relation/admin/${!this.dataForm.id ? 'save' : 'update'}`, {  
              'id': this.dataForm.id || undefined,  
              'productId': this.dataForm.productId,  
              'sessionId': this.dataForm.sessionId,  
              'seckillPrice': this.dataForm.seckillPrice,  
              'seckillCount': this.dataForm.seckillCount,  
              'seckillLimit': this.dataForm.seckillLimit,  
              'seckillStatus': this.dataForm.seckillStatus  
            }).then((response) => {  
              if (response.data && response.data.code === 200) {  
                this.$message({  
                  message: '操作成功',  
                  type: 'success',  
                  duration: 1500,  
                  onClose: () => {  
                    this.visible = false  
                    this.$emit('refreshDataList')  
                  }  
                })  
              } else {  
                this.$message.error(response.data.msg)  
              }  
            })
          }
        })
      }
    }
  }
</script>
